package com.fick.struts.action;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.fick.hibernate.dao.LinkClassDAO;
import com.fick.hibernate.dao.LinkDAO;
import com.fick.hibernate.model.Link;
import com.fick.hibernate.model.LinkClass;
import com.fick.tools.SystemLogger;
import com.fick.util.General;
import com.fick.util.UpPic;

/*
 * isPic字段使用说明
 * 888为浮动图片
 * 1 2 3 4 友情连接占用
 * 666为弹出窗口
 * */
public class ManagerLinkAction extends DispatchAction {

	// 添加友情连接 浮动广告
	public ActionForward addLink(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		SystemLogger.operatelog(request, "添加友情链接");
		HttpSession session = request.getSession();
		// 定义变量
		Link link = new Link();
		LinkDAO linkDao = new LinkDAO();

		try {

			// 定义变量
			UpPic uppic = new UpPic();

			// 配置上传环境
			uppic.setObjectPath(session.getServletContext().getRealPath("/UpLinkPic/") + "/");
			uppic.setSize(10000 * 1024);
			uppic.setSuffix(".gif.jpg.png.jpge");
			uppic.setSourceFile(request);
			String[] saObjectFile = uppic.getObjectFileName();
			int iCount = uppic.getCount();
			// 判断是否上传图片
			String picName = "";

			String sObjectPath = uppic.getObjectPath();
			// 得到参数

			String webName = uppic.getFieldValue("webName");
			String webURL = uppic.getFieldValue("webURL");
			String order = uppic.getFieldValue("linkOrder");
			Date date;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			try {
				date = sdf.parse(uppic.getFieldValue("toDate"));
			} catch (Exception e1) {
				date = new Date();
				String dateStr = sdf.format(date);
				date = sdf.parse(dateStr);
			}
			int linkOrder = 0;
			try {
				linkOrder = Integer.parseInt(order);
			} catch (Exception e) {
				linkOrder = 0;
			}

			link.setWebName(webName);
			link.setWebUrl(webURL);
			link.setLinkOrder(linkOrder);
			link.setToDate(date);
			if (saObjectFile[0] != null) {
				picName = General.convertISOToUTF("UpLinkPic/" + saObjectFile[0]);
				link.setImg(picName);
			}
			link.setIsPic(uppic.getFieldValue("type")); // 原系统为 文字连接 添加更改为下拉连接
			if (link.getIsPic().equals("3")) {
				link.setParaid(new LinkDAO().findById(Integer.parseInt(uppic.getFieldValue("paraid"))));
			}
			try {
				LinkClassDAO dao = new LinkClassDAO();
				LinkClass linkClass = dao.findById(Integer.parseInt(uppic.getFieldValue("classid")));
				link.setLinkClass(linkClass);
			} catch (Exception e) {
			}
			linkDao.save(link);
			if (link.getIsPic().equals("888")) {
				SystemLogger.operatelog(request, "添加浮动窗口成功");
				session.setAttribute("message", "添加浮动窗口成功");
				return mapping.findForward("managerAdver");
			} else {
				SystemLogger.operatelog(request, "添加友情链接成功" + link.getWebName());
				session.setAttribute("message", "添加友情链接成功");
				response.sendRedirect("/admin/managerLink.jsp?type=" + uppic.getFieldValue("type"));
				return null;
			}

		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "友情链接", "添加友情链接错误");
		}
		return null;
	}

	// 添加弹出窗口
	public ActionForward addPopAdv(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		SystemLogger.operatelog(request, "添加弹出窗口");
		HttpSession session = request.getSession();
		// 定义变量
		Link link = new Link();
		LinkDAO linkDao = new LinkDAO();

		try {

			// 定义变量
			UpPic uppic = new UpPic();

			// 配置上传环境
			uppic.setObjectPath(session.getServletContext().getRealPath("/UpLinkPic/") + "/");
			uppic.setSize(10000 * 1024);
			uppic.setSuffix(".gif.jpg.png.jpge.html.htm.rar.swf.doc.xls");
			uppic.setSourceFile(request);
			String[] saObjectFile = uppic.getObjectFileName();
			int iCount = uppic.getCount();
			// 判断是否上传图片
			String picName = "";

			String sObjectPath = uppic.getObjectPath();
			// 得到参数

			String webName = new String(uppic.getFieldValue("webName").getBytes(), "UTF-8");
			String webURL = new String(uppic.getFieldValue("webURL").getBytes(), "UTF-8");

			link.setWebName(webName);
			link.setWebUrl(webURL);
			link.setLinkOrder(0);
			if (saObjectFile[0] != null) {
				picName = General.convertISOToUTF("UpLinkPic/" + saObjectFile[0]);
				link.setImg(picName);
			}
			link.setIsPic("666"); // 由于有连接占用字段 所以将图片的字段设为一个特殊字段

			linkDao.save(link);

			SystemLogger.operatelog(request, "添加弹出窗口成功" + link.getWebName());
			session.setAttribute("message", "添加弹出窗口成功");
			return mapping.findForward("managerPop");

		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "弹出窗口", "添加弹出窗口错误");
		}
		return null;
	}

	// 修改弹出窗口

	// 删除一个链接 浮动广告
	public ActionForward delById(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		SystemLogger.operatelog(request, "删除单个链接");
		HttpSession session = request.getSession();
		int id = Integer.parseInt(request.getParameter("linkid"));
		String isadv = request.getParameter("isAdv");
		// 定义变量
		Link link = new Link();
		LinkDAO linkDao = new LinkDAO();
		try {
			// 操作数据库
			link = linkDao.findById(id);
			if (link != null) {
				if (link.getImg() != null) {
					File file = new File(session.getServletContext().getRealPath("/" + link.getImg()) + "/");
					if (file.exists()) {
						file.delete();
					}
				}
				linkDao.delete(link);
			}
			if (isadv == null) {
				SystemLogger.operatelog(request, "删除单个链接成功");
				session.setAttribute("message", "删除链接成功");
				response.sendRedirect("/admin/managerLink.jsp?type=" + request.getParameter("type"));
				return null;
			} else {
				SystemLogger.operatelog(request, "删除单个浮动窗口成功");
				session.setAttribute("message", "删除浮动窗口成功");
				return mapping.findForward("managerAdver");
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "链接", "删除链接失败");
			return null;
		}
	}

	// 删除多个友情链接 浮动广告
	public ActionForward delLinkByStr(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		SystemLogger.operatelog(request, "删除多个友情链接");
		HttpSession session = request.getSession();
		String str = request.getParameter("str");

		LinkDAO linkDao = new LinkDAO();
		Link link = new Link();
		String[] ids = str.split(",");
		int id = 0;
		try {
			for (int i = 0; i < ids.length; i++) {
				id = Integer.parseInt(ids[i]);
				link = linkDao.findById(id);
				if (link != null) {
					if (link.getImg() != null) {
						File file = new File(session.getServletContext().getRealPath("/" + link.getImg()) + "/");
						if (file.exists()) {
							file.delete();
						}
					}
					linkDao.delete(link);
				}
			}

			session.setAttribute("message", "删除成功");
			SystemLogger.operatelog(request, "删除友情链接成功");
			response.sendRedirect("/admin/managerLink.jsp?type=" + request.getParameter("type"));
			return null;
		} catch (RuntimeException e) {
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "新闻", "删除链接失败");
			return null;
		}
	}

	// 修改一个链接 浮动广告
	public ActionForward upLink(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		SystemLogger.operatelog(request, "修改链接");
		HttpSession session = request.getSession();
		// 定义变量
		Link link = new Link();
		LinkDAO linkDao = new LinkDAO();

		try {
			// 定义变量
			UpPic uppic = new UpPic();
			// 配置上传环境
			uppic.setObjectPath(session.getServletContext().getRealPath("/UpLinkPic/") + "/");
			uppic.setSize(10000 * 1024);
			uppic.setSuffix(".gif.jpg.png.jpge");
			uppic.setSourceFile(request);
			String[] saObjectFile = uppic.getObjectFileName();
			int iCount = uppic.getCount();
			// 判断是否上传图片
			String picName = "";

			int linkid = Integer.parseInt(General.convertISOToUTF(General.convertNullToEmpty(uppic.getFieldValue("id"))));

			link = linkDao.findById(linkid);
			String isadv = uppic.getFieldValue("isAdv");
			if (saObjectFile[0] != null) {
				// 删除原来图片
				File file = new File(session.getServletContext().getRealPath("/" + link.getImg()) + "/");
				if (file.exists()) {
					file.delete();
				}
				picName = General.convertISOToUTF("UpLinkPic/" + saObjectFile[0]);
				link.setImg(picName);
			}
			link.setIsPic(uppic.getFieldValue("type")); // 原系统为 文字连接 添加更改为下拉连接

			if (link.getIsPic().equals("3")) // 当为下接链接时执行
			{
				link.setParaid(new LinkDAO().findById(Integer.parseInt(uppic.getFieldValue("paraid"))));
			}
			try {
				LinkClassDAO dao = new LinkClassDAO();
				LinkClass linkClass = dao.findById(Integer.parseInt(uppic.getFieldValue("classid")));
				link.setLinkClass(linkClass);
			} catch (Exception e) {
			}
			String webName = uppic.getFieldValue("webName");
			String webURL = uppic.getFieldValue("webURL");
			Date date;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			try {
				date = sdf.parse(uppic.getFieldValue("toDate"));
			} catch (Exception e1) {
				date = new Date();
				String dateStr = sdf.format(date);
				date = sdf.parse(dateStr);
			}
			String order = uppic.getFieldValue("linkOrder");
			int linkOrder = 0;
			try {
				linkOrder = Integer.parseInt(order);
			} catch (Exception e) {
				linkOrder = 0;
			}
			link.setLinkOrder(linkOrder);

			link.setWebName(webName);
			link.setWebUrl(webURL);
			link.setToDate(date);
			linkDao.update(link);
			if (link.getIsPic() != null && link.getIsPic().equals("888")) {
				SystemLogger.operatelog(request, "修改浮动窗口成功");
				session.setAttribute("message", "修改浮动窗口成功");
				return mapping.findForward("managerAdver");
			} else {
				SystemLogger.operatelog(request, "修改链接成功" + link.getWebName());
				session.setAttribute("message", "修改链接成功");
				response.sendRedirect("/admin/managerLink.jsp?type=" + uppic.getFieldValue("type"));
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "链接", "修改链接错误");
		}
		return null;
	}

	public ActionForward upPopAdv(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		SystemLogger.operatelog(request, "修改链接");
		HttpSession session = request.getSession();
		// 定义变量
		Link link = new Link();
		LinkDAO linkDao = new LinkDAO();

		try {
			// 定义变量
			UpPic uppic = new UpPic();
			// 配置上传环境
			uppic.setObjectPath(session.getServletContext().getRealPath("/UpLinkPic/") + "/");
			uppic.setSize(10000 * 1024);
			uppic.setSuffix(".gif.jpg.png.jpge.html.htm.rar.swf.doc.xls");
			uppic.setSourceFile(request);
			String[] saObjectFile = uppic.getObjectFileName();
			int iCount = uppic.getCount();
			// 判断是否上传图片
			String picName = "";

			int linkid = Integer.parseInt(General.convertISOToUTF(General.convertNullToEmpty(uppic.getFieldValue("linkid"))));

			link = linkDao.findById(linkid);
			if (saObjectFile[0] != null) {
				picName = General.convertISOToUTF("UpLinkPic/" + saObjectFile[0]);
				link.setImg(picName);
			}

			link.setIsPic("666");

			String webName = uppic.getFieldValue("webName");
			String webURL = uppic.getFieldValue("webURL");

			link.setWebName(webName);
			link.setWebUrl(webURL);
			linkDao.update(link);

			SystemLogger.operatelog(request, "修改弹出窗口成功");
			session.setAttribute("message", "修改弹出窗口成功");
			return mapping.findForward("managerPop");

		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "弹出窗口", "修改弹出窗口错误");
		}
		return null;
	}
}
